#pragma GCC optimize(2)
#include <iostream>
#include <algorithm>
#include <cstring>
#include <string>
#include <map>
#include <unordered_map>
#include <vector>
#include <set>
#include <queue>

using namespace std;
using LL = long long;
using PII = pair<int, int>;

const int N = 2e5 + 10;

LL n, m;
LL A[N];
vector<LL> nums;
unordered_map<LL, LL> mp;
LL sum = 0;

int main(){
    freopen("in.txt", "r", stdin);
    freopen("out.txt", "w", stdout);
    ios::sync_with_stdio(false);
    cin.tie(nullptr);

    cin >> n >> m;
    for(int i = 1; i <= n; i ++){
        cin >> A[i];
        
        if(mp.count(A[i])) mp[A[i]] ++;
        else mp[A[i]] = 1;
        nums.push_back(A[i]);
        sum += A[i];
    }
    // cout << sum << '\n';

    sort(nums.begin(), nums.end());
    nums.erase(unique(nums.begin(), nums.end()), nums.end());

    LL last = 0;
    LL maxv = 0;
    LL cursum = 0;
    bool fo = true;

    if(nums.back() == m - 1 && nums[0] == 0) fo = false;

    LL sumfo = 0;
    for(int i = 0; i < nums.size(); i ++){
        if((nums[i] != last) && (nums[i] != (last + 1))){
            // cout << (nums[i] != last) << " " <<  (nums[i] != (last + 1)) << " " << last << " " << nums[i] << '\n';
            if(fo){
                maxv = max(maxv, cursum);
                last = nums[i];
                cursum = 0;
                cursum += nums[i] * mp[nums[i]];
                continue;
            }else{
                fo = true;
                maxv = max(maxv, cursum);
                sumfo = maxv;
                last = nums[i];
                cursum = 0;
                cursum += nums[i] * mp[nums[i]];
                continue;
            }
        }

        cursum += mp[nums[i]] * nums[i];
        last = nums[i];
    }
    // cout << "cur: " << cursum << '\n';
    maxv = max(maxv, cursum + sumfo);

    cout << sum - maxv << '\n'; 


    return 0;
}